﻿namespace QLPhongKham.UC
{
    using ConfigDatabase.Class;
    using DevExpress.Utils;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraTab;
    using QLPhongKham.BaseClass.Chung;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class UCquanlydangnhap_edit : XtraUserControl
    {
        private bool _bdoimatkhau = false;
        private ConnectDatabase _condata;
        private dtoDMDangNhap _dtodangnhap;
        private SimpleButton btnketthuc;
        private SimpleButton btnluu;
        private LookUpEdit cbbnhom;
        private IContainer components = null;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private TextEdit txtdangnhap;
        private TextEdit txtmatkhau;
        private TextEdit txtmatkhau2;
        private TextEdit txtnguoidung;

        public UCquanlydangnhap_edit()
        {
            this.InitializeComponent();
            this._dtodangnhap = new dtoDMDangNhap();
        }

        private void btnketthuc_Click(object sender, EventArgs e)
        {
            try
            {
                XtraTabPage parent = (XtraTabPage) base.Parent;
                XtraTabControl control = (XtraTabControl) parent.Parent;
                control.TabPages.Remove(parent);
            }
            catch
            {
                ((Form) base.Parent).Close();
            }
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            if (this.f_kiemtraluu())
            {
                this._dtodangnhap.IDCHUONGTRINH = 1;
                this._dtodangnhap.MATKHAU = this.txtmatkhau.Text;
                this._dtodangnhap.NHOMSUDUNG = int.Parse(this.cbbnhom.EditValue.ToString());
                this._dtodangnhap.TENDANGNHAP = this.txtdangnhap.Text;
                this._dtodangnhap.TENNGUOIDUNG = this.txtnguoidung.Text;
                if (this._dtodangnhap.ID == 0)
                {
                    this._dtodangnhap.ID = this._dtodangnhap.GetIDInsert();
                    if (!this._dtodangnhap.Insert())
                    {
                        MessageBox.Show("Lưu th\x00f4ng tin đăng nhập kh\x00f4ng th\x00e0nh c\x00f4ng.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;
                    }
                }
                else if (!this._dtodangnhap.Update())
                {
                    MessageBox.Show("Lưu th\x00f4ng tin đăng nhập kh\x00f4ng th\x00e0nh c\x00f4ng.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
                MessageBox.Show("Lưu th\x00e0nh c\x00f4ng.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void f_doimatkhau()
        {
            this.txtnguoidung.Enabled = this.txtdangnhap.Enabled = this.cbbnhom.Enabled = !this._bdoimatkhau;
        }

        private bool f_kiemtraluu()
        {
            if (this.txtnguoidung.Text == "")
            {
                MessageBox.Show("Nhập t\x00ean người d\x00f9ng.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.txtnguoidung.Focus();
                return false;
            }
            if (this.txtdangnhap.Text == "")
            {
                MessageBox.Show("Nhập t\x00ean đăng nhập.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.txtdangnhap.Focus();
                return false;
            }
            if (((this.txtmatkhau2.Text != "") && (this.txtmatkhau.Text != "")) && (this.txtmatkhau2.Text != this.txtmatkhau.Text))
            {
                MessageBox.Show("Nhập mật khẩu kh\x00f4ng đ\x00fang.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.txtmatkhau.Focus();
                return false;
            }
            if (this.cbbnhom.EditValue == null)
            {
                MessageBox.Show("Chọn nh\x00f3m sử dụng.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.cbbnhom.Focus();
                return false;
            }
            return true;
        }

        private void f_loadcontrol_dangnhap(string tennguoidung, string tendangnhap, string matkhau, string matkhau2, int nhomsudung)
        {
            this.txtnguoidung.Text = tennguoidung;
            this.txtdangnhap.Text = tendangnhap;
            this.txtmatkhau.Text = matkhau;
            this.txtmatkhau2.Text = matkhau2;
            this.cbbnhom.EditValue = nhomsudung;
        }

        private DataTable f_loaddata_nhomsudung()
        {
            DataTable table = new DataTable();
            table.Columns.Add("id", typeof(int));
            table.Columns.Add("ten");
            table.Rows.Add(new object[] { 1, "Quản trị hệ thống" });
            table.Rows.Add(new object[] { 2, "Người sử dụng" });
            return table;
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(UCquanlydangnhap_edit));
            this.btnketthuc = new SimpleButton();
            this.btnluu = new SimpleButton();
            this.label1 = new Label();
            this.txtnguoidung = new TextEdit();
            this.label2 = new Label();
            this.txtdangnhap = new TextEdit();
            this.label3 = new Label();
            this.txtmatkhau = new TextEdit();
            this.label4 = new Label();
            this.txtmatkhau2 = new TextEdit();
            this.label5 = new Label();
            this.cbbnhom = new LookUpEdit();
            this.txtnguoidung.Properties.BeginInit();
            this.txtdangnhap.Properties.BeginInit();
            this.txtmatkhau.Properties.BeginInit();
            this.txtmatkhau2.Properties.BeginInit();
            this.cbbnhom.Properties.BeginInit();
            base.SuspendLayout();
            this.btnketthuc.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btnketthuc.Appearance.Options.UseTextOptions = true;
            this.btnketthuc.Appearance.TextOptions.HAlignment = HorzAlignment.Near;
            this.btnketthuc.Image = Properties.Resources.imgclose;// (Image)manager.GetObject("btnketthuc.Image");
            this.btnketthuc.Location = new Point(0xc3, 0x8f);
            this.btnketthuc.Name = "btnketthuc";
            this.btnketthuc.Size = new Size(0x6a, 0x17);
            this.btnketthuc.TabIndex = 6;
            this.btnketthuc.Text = " &Kết th\x00fac [F12]";
            this.btnketthuc.Click += new EventHandler(this.btnketthuc_Click);
            this.btnluu.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.btnluu.Appearance.Options.UseTextOptions = true;
            this.btnluu.Appearance.TextOptions.HAlignment = HorzAlignment.Near;
            this.btnluu.Image = Properties.Resources.imgsave;// (Image)manager.GetObject("btnluu.Image");
            this.btnluu.Location = new Point(0x71, 0x8f);
            this.btnluu.Name = "btnluu";
            this.btnluu.Size = new Size(0x4e, 0x17);
            this.btnluu.TabIndex = 5;
            this.btnluu.Text = " &Lưu [F5]";
            this.btnluu.Click += new EventHandler(this.btnluu_Click);
            this.label1.Font = new Font("Tahoma", 10f);
            this.label1.Location = new Point(3, 10);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x71, 0x13);
            this.label1.TabIndex = 0x16;
            this.label1.Text = "T\x00ean người d\x00f9ng:";
            this.label1.TextAlign = ContentAlignment.TopRight;
            this.txtnguoidung.Location = new Point(0x71, 9);
            this.txtnguoidung.Name = "txtnguoidung";
            this.txtnguoidung.Properties.CharacterCasing = CharacterCasing.Upper;
            this.txtnguoidung.Properties.MaxLength = 0xbb8;
            this.txtnguoidung.Size = new Size(230, 20);
            this.txtnguoidung.TabIndex = 0;
            this.txtnguoidung.KeyDown += new KeyEventHandler(this.UCquanlydangnhap_edit_KeyDown);
            this.label2.Font = new Font("Tahoma", 10f);
            this.label2.Location = new Point(3, 0x24);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x71, 0x13);
            this.label2.TabIndex = 0x16;
            this.label2.Text = "T\x00ean đăng nhập:";
            this.label2.TextAlign = ContentAlignment.TopRight;
            this.txtdangnhap.Location = new Point(0x71, 0x23);
            this.txtdangnhap.Name = "txtdangnhap";
            this.txtdangnhap.Properties.MaxLength = 100;
            this.txtdangnhap.Size = new Size(230, 20);
            this.txtdangnhap.TabIndex = 1;
            this.txtdangnhap.KeyDown += new KeyEventHandler(this.UCquanlydangnhap_edit_KeyDown);
            this.label3.Font = new Font("Tahoma", 10f);
            this.label3.Location = new Point(3, 0x3e);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x71, 0x13);
            this.label3.TabIndex = 0x16;
            this.label3.Text = "Mật khẩu:";
            this.label3.TextAlign = ContentAlignment.TopRight;
            this.txtmatkhau.Location = new Point(0x71, 0x3d);
            this.txtmatkhau.Name = "txtmatkhau";
            this.txtmatkhau.Properties.MaxLength = 0xbb8;
            this.txtmatkhau.Properties.PasswordChar = '*';
            this.txtmatkhau.Size = new Size(230, 20);
            this.txtmatkhau.TabIndex = 2;
            this.txtmatkhau.KeyDown += new KeyEventHandler(this.UCquanlydangnhap_edit_KeyDown);
            this.label4.Font = new Font("Tahoma", 10f);
            this.label4.Location = new Point(3, 0x58);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x71, 0x13);
            this.label4.TabIndex = 0x16;
            this.label4.Text = "Lặp lại mật khẩu:";
            this.label4.TextAlign = ContentAlignment.TopRight;
            this.txtmatkhau2.Location = new Point(0x71, 0x57);
            this.txtmatkhau2.Name = "txtmatkhau2";
            this.txtmatkhau2.Properties.MaxLength = 0xbb8;
            this.txtmatkhau2.Properties.PasswordChar = '*';
            this.txtmatkhau2.Size = new Size(230, 20);
            this.txtmatkhau2.TabIndex = 3;
            this.txtmatkhau2.KeyDown += new KeyEventHandler(this.UCquanlydangnhap_edit_KeyDown);
            this.txtmatkhau2.Validated += new EventHandler(this.txtmatkhau2_Validated);
            this.label5.Font = new Font("Tahoma", 10f);
            this.label5.Location = new Point(3, 0x74);
            this.label5.Name = "label5";
            this.label5.Size = new Size(0x71, 0x13);
            this.label5.TabIndex = 0x16;
            this.label5.Text = "Nh\x00f3m sử dụng:";
            this.label5.TextAlign = ContentAlignment.TopRight;
            this.cbbnhom.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.cbbnhom.Location = new Point(0x71, 0x71);
            this.cbbnhom.Name = "cbbnhom";
            this.cbbnhom.Properties.AllowDropDownWhenReadOnly = DefaultBoolean.True;
            this.cbbnhom.Properties.Appearance.Font = new Font("Tahoma", 10f);
            this.cbbnhom.Properties.Appearance.Options.UseFont = true;
            this.cbbnhom.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.cbbnhom.Properties.Columns.AddRange(new LookUpColumnInfo[] { new LookUpColumnInfo("id", "id", 20, FormatType.None, "", false, HorzAlignment.Default), new LookUpColumnInfo("ten", "T\x00ean nh\x00f3m") });
            this.cbbnhom.Properties.NullText = "";
            this.cbbnhom.Size = new Size(230, 0x16);
            this.cbbnhom.TabIndex = 4;
            this.cbbnhom.KeyDown += new KeyEventHandler(this.UCquanlydangnhap_edit_KeyDown);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.cbbnhom);
            base.Controls.Add(this.txtmatkhau2);
            base.Controls.Add(this.label5);
            base.Controls.Add(this.label4);
            base.Controls.Add(this.txtmatkhau);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.txtdangnhap);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.txtnguoidung);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.btnluu);
            base.Controls.Add(this.btnketthuc);
            this.LookAndFeel.UseWindowsXPTheme = true;
            base.Name = "UCquanlydangnhap_edit";
            base.Size = new Size(0x164, 0xa9);
            base.Load += new EventHandler(this.UCphanquyensudung_Load);
            base.KeyDown += new KeyEventHandler(this.UCquanlydangnhap_edit_KeyDown);
            this.txtnguoidung.Properties.EndInit();
            this.txtdangnhap.Properties.EndInit();
            this.txtmatkhau.Properties.EndInit();
            this.txtmatkhau2.Properties.EndInit();
            this.cbbnhom.Properties.EndInit();
            base.ResumeLayout(false);
        }

        public virtual void stbnketthuc_Click(object sender, EventArgs e)
        {
            XtraTabPage parent = (XtraTabPage) base.Parent;
            XtraTabControl control = (XtraTabControl) parent.Parent;
            control.TabPages.Remove(parent);
        }

        private void txtmatkhau2_Validated(object sender, EventArgs e)
        {
        }

        private void UCphanquyensudung_Load(object sender, EventArgs e)
        {
            if (this._condata == null)
            {
                this._condata = new ConnectDatabase();
            }
            this.cbbnhom.Properties.DataSource = this.f_loaddata_nhomsudung();
            this.cbbnhom.Properties.DisplayMember = "ten";
            this.cbbnhom.Properties.ValueMember = "id";
            this.f_loadcontrol_dangnhap(this._dtodangnhap.TENNGUOIDUNG, this._dtodangnhap.TENDANGNHAP, this._dtodangnhap.MATKHAU, this._dtodangnhap.MATKHAU, this._dtodangnhap.NHOMSUDUNG);
            this.f_doimatkhau();
        }

        private void UCquanlydangnhap_edit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        public ConnectDatabase pCondata
        {
            get
            {
                return this._condata;
            }
            set
            {
                this._condata = value;
            }
        }

        public dtoDMDangNhap pDangNhap
        {
            get
            {
                return this._dtodangnhap;
            }
            set
            {
                this._dtodangnhap = value;
            }
        }

        public bool pDoiMatKhau
        {
            get
            {
                return this._bdoimatkhau;
            }
            set
            {
                this._bdoimatkhau = value;
            }
        }
    }
}

